/* 
*** Downstream effects by representation of women in the union (amenities data)
	// input: filledpanel, unionboards_SDSR_clean, estabchars_wide
	// output: TableA15.xlsx (sheet: amenities)
*/ 

cap log close
cap log using "$logs/amenities_heterog1_log", replace


*****************
** CBA CLAUSES **
*****************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel
	

	*** Data prep 
	* Get baseline female share in union boards and a dummy for female Pres/VP
	use "$files/unionboards_SDSR_clean.dta", clear
	keep union_id sh_female_bl female_president_bl female_vicepresident_bl union_type
	//tag unions with info on boards at baseline 
	gen has_bl = (sh_female_bl!=. & female_president_bl!=.)
	duplicates drop
	sum sh_female_bl, d 
	sum sh_female_bl if union_type=="Trabalhador", d 
	gen high_sh_female_bl = (sh_female_bl>1/3) if sh_female_bl!=.
	egen female_PorVP = rowmax( female_president_bl female_vicepresident_bl)
	tab high_sh_female_bl
	tab high_sh_female_bl if union_type=="Trabalhador"
	tab female_PorVP
	tab female_PorVP if union_type=="Trabalhador"
	tempfile baselines
	save `baselines' 
	

	* Use contract data
	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	* merge with union bl share/bl female pres-VP gender 
	cap drop _merge
	merge m:1 union_id  using `baselines' , keep(1 3)
	drop _merge
	
	* merge with estab characteristics to get share of female employment
	merge m:1 employer_id using "$files/estabchars_wide", keepusing(tot_fememployment2014 tot_employment2014)
	drop if _m == 2
	drop _merge
	replace tot_fememployment2014 = 0 if tot_fememployment2014 == . & tot_employment2014 !=.
	gen sharefemale_2014 = tot_fememployment2014/tot_employment2014
	egen unique_emp = tag(employer_id)
	sum sharefemale_2014 if unique_emp==1, d 
	local median = r(p50)
	gen high_sharefemale_2014 = (sharefemale_2014>`median') if sharefemale_2014!=.
	//for reference on baseline union characteristics restricted to the estimation sample
	egen unique_uni = tag(union_id)
	sum sh_female_bl if unique_uni==1, d 
	tab high_sh_female_bl if unique_emp==1
	tab female_PorVP if unique_emp==1

	* time varying fixed effects
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}
	
	* for clustering
	egen clustergrp = group(`clstr')
		
	* SOME COLUMNS ON TABLE WILL BE RESTRICTED TO HAVING INFO ON BOARD AT BASELINE
	tab has_bl
	unique union_id
	unique union_id if has_bl!=1 
	unique estab_union_pair
	unique estab_union_pair if has_bl!=1 
	mdesc high_sh_female_bl female_PorVP high_sharefemale_2014
	replace female_sh = 0 if female_sh == . 
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'
	
	***FEMALE***
	* Columns 1: Main results
	************************************************************

	local depvar female_clauses_DD female_clauses_DD_b any_female_cl_DD female_cl_DD_sh

	local j = 1
	foreach var of local depvar {	
			
			*** All signing firms
			reghdfe `var' treatpost , absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			estadd scalar sumdum = 0
			estadd scalar sumdump = 0
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store a1`j'
												
			local ++j
				
			}
	
	
	* Column 2 : interact with low female share (union boards)
	************************************************************

	local depvar female_clauses_DD female_clauses_DD_b any_female_cl_DD female_cl_DD_sh
	gen dummy = (high_sh_female_bl==0) if high_sh_female_bl!=.
	
	local j = 1
	foreach var of local depvar {	
			
			*** All signing firms
			reghdfe `var' treatpost##dummy treat##dummy post##dummy, absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store b1`j'
												
			local ++j
				
			}
	drop dummy	
		
		
			
	*** Export ***
	forvalues j = 1/4 {
			#d ;
			estout a1`j' b1`j' using "$tables/T4_1het_`j'.txt", 
			style(tab) mlabels(none) label collabels(none)
			cells(b(star fmt(%9.3f)) se(par)) 
			stats(sumdum sumdump meandv obs, fmt(%9.3f %9.3f %9.2fc %9.0fc ) labels("sum" "p-value" "mean depvar" "N" )) 
			drop(o.*, relax) keep(treatpost 1.treatpost 1.treatpost#1.dummy, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
			#d cr
		}	
		
				
	local lab1 Intensive_margin
	local lab2 Intensive_margin_binary
	local lab3 Extensive_margin
	local lab4 Share
	
	local r1 = 1
	local r2 = 12 
	local r3 = 24 
	local r4 = 36
	
	
	import delimited "$tables/T4_1het_1.txt", clear
	rename v1 `lab1'
	rename v2 main
	rename v3 low_femunion

			
	export excel using "$tables/TableA15.xlsx", sheet("amenities") cell(A`r1') firstrow(var) sheetreplace

	
	forvalues p = 2/4 {
	
	import delimited "$tables/T4_1het_`p'.txt", clear
	label var v1 `lab`p''
	label var v2 " "
	label var v3 " "
			
	export excel using "$tables/TableA15.xlsx", sheet("amenities") cell(A`r`p'') firstrow(varl) sheetmodify

	}
		

cap log close
